package su.mvc.action.image;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.convention.annotation.*;
import su.mvc.action.ActionSupport;
import su.mvc.model.Image;
import su.mvc.service.gui.ImageService;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

@Results(value = {
        @Result(name = ActionSupport.SUCCESS, location = "upload-form.jsp"),
        @Result(name = ActionSupport.REDIRECT, type = "redirect", location = "/image/upload.xhtml")
})
@ParentPackage("default")
public class UploadAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(UploadAction.class.getName());
    
    private ImageService imageService;


    private List<String> uploads = new ArrayList<String>();
    private List<String> uploadFileNames = new ArrayList<String>();
    private List<String> uploadContentTypes = new ArrayList<String>();
    private List<InputStream> uploadStreams = new ArrayList<InputStream>();

    public ImageService getImageService() {
        return imageService;
    }

    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    public List<InputStream> getUploadStreams() {
        return uploadStreams;
    }

    public void setUploadStreams(List<InputStream> uploadStreams) {
        this.uploadStreams = uploadStreams;
    }

    public List<String> getUpload() {
        return uploads;
    }

    public void setUpload(List<String> uploads) {
        this.uploads = uploads;
    }

    public List<String> getUploadFileName() {
        return this.uploadFileNames;
    }
    public void setUploadFileName(List<String> uploadFileNames) {
        this.uploadFileNames = uploadFileNames;
    }

    public List<String> getUploadContentType() {
        return this.uploadContentTypes;
    }
    public void setUploadContentType(List<String> contentTypes) {
        this.uploadContentTypes = contentTypes;
    }

    @Action(interceptorRefs=@InterceptorRef("gaeFileUploadStack"))
    public String upload() throws Exception {
        imageService.saveImages(getUpload(), getUploadFileName(), getUploadContentType());
        return ActionSupport.REDIRECT;
    }

}
